/**
 * Created by huanlai.whl on 2017/3/15.
 */
function ScrollManager(navigationWrapper, contentWrapper){
    console.log(' comehere -------- ');
    this.contentArray = new Array();
    this.navigatView = navigationWrapper;
    this.contentView = contentWrapper;
    this.navigatID = 'navagationid_';
    this.contentID = 'contentid_';
    console.log(this.initView);
    console.log('从来就没有救世主!!!')
};

ScrollManager.prototype.initView = function(jsonData){
    //console.log(jsonData);
    this.allDataArray = jsonData;
    var length = this.allDataArray.length;
    var navagationItem = "<p class=\"navigation-item\" fe-role=\"Widget\"></p>";
    for(var i=0; i<length; i++){
        var displayId = i;
        var data = this.allDataArray[i];
        var navagationChild = $(navagationItem).get(0);
        navagationChild.id = this.navigatID + displayId;
        navagationChild.innerHTML = data.menuname;
        var contentChild = undefined;
        if("musiclist" == data.menutype){
            contentChild = new ContentView(ContentView.Type.MUSICLIST);
        } else {
            contentChild = new ContentView(ContentView.Type.ALBUM);
        }
        contentChild.setData(data);
        contentChild.getDomImpl().id = this.contentID + displayId;
        this.contentArray.push(contentChild);
        this.navigatView.appendChild(navagationChild);
        this.contentView.appendChild(contentChild.getDomImpl());
    }
    FocusEngine.render();
    FocusEngine.getWidgetById(this.contentID + 0).cache();
    this.navigationScroll = FocusEngine.getWidgetById("navigation-wrapper");
    this.contentScroll = FocusEngine.getWidgetById("content-wrapper");
    this.registListener();
};

ScrollManager.prototype.registListener = function(){
    this.navigationScroll.on("focus",function(e){
        console.log("navigationScroll == " + e.target.id);
        this.alignContentAndNavigator(e.target.id);
    }.bind(this));

    this.contentScroll.on("focus",function(e){
        this.alignContentAndNavigator(e.target.parentWidget.id);
    }.bind(this));

    this.contentScroll.on("ok", function(e){
        console.log(e);
        this.handleContentItemClicked(e);
    }.bind(this));
};

ScrollManager.prototype.alignContentAndNavigator = function(currentID){
    console.log("currentID == " + currentID);
    if(currentID.indexOf(this.navigatID) != -1){
        var index = currentID.replace(this.navigatID,"");
        if(this.lastScrollIndex && this.lastScrollIndex == index){
            return;
        }
        var widget = FocusEngine.getWidgetById(this.contentID + index);
        widget.cache();
        widget.getFirstFocusableChildWidget().cache();
        this.contentScroll.scrollToWidget(widget);
        if(this.lastScrollIndex){
            this.contentArray[this.lastScrollIndex].blur();
        }
        this.lastScrollIndex = index;
    } else if(currentID.indexOf(this.contentID) != -1){
        var index = currentID.replace(this.contentID,"");
        if(this.lastScrollIndex && this.lastScrollIndex == index){
            return;
        }
        var widget = FocusEngine.getWidgetById(this.navigatID + index);
        widget.cache();
        this.navigationScroll.scrollToWidget(widget);
        if(this.lastScrollIndex){
            this.contentArray[this.lastScrollIndex].blur();
        }
        this.lastScrollIndex = index;
    }
};

ScrollManager.prototype.handleContentItemClicked = function(e){
    var parentId = e.target.parentWidget.id;
    console.log("parentId == " + parentId);
    var index = parentId.replace(this.contentID,"");
    this.contentArray[index].onClicked(e);
};
